/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package GUI; import JMS.JmsConsumerP2P; import JMS.JmsConsumerP2P.Types; import JMS.JmsProducerP2P; import Others.Echiquier; import ant.Joueur; import ant.Piece; import ant.Plateau; import ejb.SessionBeanRemote; import java.awt.Color; import java.awt.GridLayout; import java.awt.Point; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; import javax.swing.BorderFactory; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.border.Border; /** * * @author Greenlamp */ public class Main extends javax.swing.JFrame implements MessageListener{ SessionBeanRemote sessionBean; /** * Creates new form Main */ Echiquier[][] plateau; JPanel chessboard; String nomPlateau = null; Lobby parent; boolean waitAdversaire; String idMessage; String idMessageAdverse; JmsProducerP2P producer; JmsConsumerP2P consumer; Joueur joueur; List<Point> deplacementsPossible; Piece selected; boolean myTurn; boolean onEchec; public enum MSG{ NOUVEAUJOUEUR, JOUEURPARTI, DEPLACEMENT, ECHEC, ECHECETMAT; public String getValue(){ return this.name().toLowerCase(); } }; public Main() { initBoard(); } Main(SessionBeanRemote sessionBean, String nomPlateau, Joueur joueur, Lobby parent) { this.setSize(480, 480); setLocationRelativeTo(null); this.nomPlateau = nomPlateau; this.sessionBean = sessionBean; this.selected = null; this.parent = parent; this.joueur = joueur; onEchec = false; this.setTitle("Plateau: " + this.nomPlateau +" Couleur: " + (joueur.getColor().getRGB() == Color.WHITE.getRGB() ? "Blanc" : "Noir")); this.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e){ quitterPartie(); } }); try { this.idMessage = nomPlateau + "-" + "j:" + joueur.getId(); System.out.println("idMessage: " + idMessage); } catch (Exception ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } producer = new JmsProducerP2P(); consumer = new JmsConsumerP2P(Types.ASYNC, this.idMessage); consumer.addListener(this); initJoueurs(); initBoard(); refreshPlateau(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 400, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 300, Short.MAX_VALUE) ); setBounds(0, 0, 416, 338); }// </editor-fold>//GEN-END:initComponents /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Main().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables // End of variables declaration//GEN-END:variables private void initBoard() { plateau = new Echiquier[8][8]; chessboard = new JPanel(new GridLayout(8, 8)); Border border = null; border = BorderFactory.createLineBorder(Color.BLACK); for(int x=0; x<8; x++){ for(int y=0; y<8; y++){ if((x + y) % 2 == 0){ plateau[x][y] = new Echiquier(this, x, y, Color.GRAY); }else{ plateau[x][y] = new Echiquier(this, x, y, Color.WHITE); } plateau[x][y].setBorder(border); chessboard.add(plateau[x][y]); } } chessboard.updateUI(); this.add(chessboard); } private void clearPlateau() { for(int x=0; x<8; x++){ for(int y=0; y<8; y++){ plateau[x][y].removePion(); } } chessboard.updateUI(); } private void refreshPlateau() { try { List<Piece> pieces = sessionBean.getPieceByPlateau(nomPlateau); for(Piece piece : pieces){ plateau[piece.getPosX()][piece.getPosY()].addPiece(piece); } chessboard.updateUI(); } catch (Exception ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } } private void quitterPartie(){ System.out.println("Quitter partie"); try { sessionBean.quitterPartie(nomPlateau, joueur.getColor()); } catch (Exception ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } if(!waitAdversaire){ System.out.println("this.producer.sendMessage(MSG.JOUEURPARTI.getValue(), idMessageAdverse);"); this.producer.sendMessage(MSG.JOUEURPARTI.getValue(), idMessageAdverse); } this.dispose(); System.exit(0); } public void onMouseClick(Echiquier caseSelected) { if(waitAdversaire){ JOptionPane.showMessageDialog(this, "Veuillez attendre un adversaire"); return; } if(myTurn){ int posX = caseSelected.getPosX(); int posY = caseSelected.getPosY(); boolean rouge = caseSelected.isSelected(); if(selected == null){ try { selected = sessionBean.getPieceAt(nomPlateau, posX, posY); } catch (Exception ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } if(selected == null){ return; } if(selected.getColor().getRGB() != joueur.getColor().getRGB()){ selected = null; return; } try { deplacementsPossible = sessionBean.getDeplacementPossible(nomPlateau, posX, posY); } catch (Exception ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } if(deplacementsPossible.isEmpty()){ selected = null; }else{ for(Point point : deplacementsPossible){ plateau[point.x][point.y].selectionne(); } } }else{ if(rouge){ for(Point point : deplacementsPossible){ plateau[point.x][point.y].selectionne(); } bouger(posX, posY); selected = null; deplacementsPossible.clear(); }else{ for(Point point : deplacementsPossible){ plateau[point.x][point.y].selectionne(); } selected = null; deplacementsPossible.clear(); } } } } public void bouger(int posX, int posY){ boolean ok = false; try { ok = sessionBean.bougerPion(nomPlateau, selected.getPosX(), selected.getPosY(), posX, posY); } catch (Exception ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } if(ok){ clearPlateau(); refreshPlateau(); this.producer.sendMessage(MSG.DEPLACEMENT.getValue(), this.idMessageAdverse); myTurn = false; } } @Override public void onMessage(Message message) { TextMessage textMessage = (TextMessage)message; String msg = null; try { msg = textMessage.getText(); System.out.println("Message reçu: " + msg); } catch (JMSException ex) { Logger.getLogger(Lobby.class.getName()).log(Level.SEVERE, null, ex); } if(msg.equalsIgnoreCase(MSG.NOUVEAUJOUEUR.getValue())){ if(waitAdversaire == true){ try { Long idAdversaire = sessionBean.getIdJoueur(nomPlateau, Color.BLACK); this.idMessageAdverse = nomPlateau + "-" + "j:" + idAdversaire; waitAdversaire = false; JOptionPane.showMessageDialog(this, "Un adversaire a rejoint la partie"); } catch (Exception ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } } }else if(msg.equalsIgnoreCase(MSG.JOUEURPARTI.getValue())){ if(!waitAdversaire){ JOptionPane.showMessageDialog(this, "L'adversaire est parti, vous êtes donc vainqueur par forfait !"); try { sessionBean.quitterPartie(nomPlateau, joueur.getColor()); } catch (Exception ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } parent.dispose(); this.dispose(); System.exit(0); } }else if(msg.equalsIgnoreCase(MSG.DEPLACEMENT.getValue())){ myTurn = true; clearPlateau(); refreshPlateau(); /*try { if(sessionBean.onEchecEtMat(nomPlateau, joueur.getColor())){ JOptionPane.showMessageDialog(this, "Echec et mat !"); } } catch (Exception ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); }*/ try { onEchec = sessionBean.onEchec(nomPlateau, joueur.getColor()); } catch (Exception ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } if(onEchec){ JOptionPane.showMessageDialog(this, "Echec"); } }else if(msg.equalsIgnoreCase(MSG.ECHECETMAT.getValue())){ refreshPlateau(); } } private void initJoueurs() { if(joueur.getColor().getRGB() == Color.BLACK.getRGB()){ try { Long idAdversaire = sessionBean.getIdJoueur(nomPlateau, Color.WHITE); this.idMessageAdverse = nomPlateau + "-" + "j:" + idAdversaire; System.out.println("idMessageAdverse: " + idMessageAdverse); } catch (Exception ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } this.producer.sendMessage(MSG.NOUVEAUJOUEUR.getValue(), this.idMessageAdverse); waitAdversaire = false; myTurn = false; }else{ myTurn = true; waitAdversaire = true; } } }